//
//  ChooseAreaViewController.m
//  Mall
//
//  Created by 孙飞燕 on 16/10/28.
//  Copyright © 2016年 孙飞燕. All rights reserved.
//

#import "ChooseAreaViewController.h"

@interface ChooseAreaViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSIndexPath *selectedIndexPath;//当前选中的NSIndexPath
@property (nonatomic,strong)UITableView *tableView;

@end

@implementation ChooseAreaViewController

- (void)setAreaData{
    if (self.displayType == kDisplayProvince) {
        //从文件读取地址字典
        NSString *addressPath = [[NSBundle mainBundle] pathForResource:@"address" ofType:@"plist"];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithContentsOfFile:addressPath];
        self.provinces = [dict objectForKey:@"address"];
        NSLog(@"3333333333%@",self.provinces);
    }
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame];

        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self setAreaData];
    
    }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.displayType == kDisplayProvince) {
        return self.provinces.count;
    }else if (self.displayType == kDisplayCity){
        return self.citys.count;
    }else{
        return self.areas.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    if (self.displayType == kDisplayProvince) {
        NSDictionary *province = self.provinces[indexPath.row];
        NSString *provinceName = [province objectForKey:@"name"];
        cell.textLabel.text = provinceName;
    }else if (self.displayType == kDisplayCity){
        NSDictionary *city = self.citys[indexPath.row];
        NSString *cityName = [city objectForKey:@"name"];
        cell.textLabel.text = cityName;
    }else{
        cell.textLabel.text = self.areas[indexPath.row];

    }
    return cell;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (self.displayType == kDisplayProvince) {
        NSDictionary *province = self.provinces[indexPath.row];
        NSArray *citys = [province objectForKey:@"sub"];
        self.selectedProvince = [province objectForKey:@"name"];
        //构建下一级视图控制器
        ChooseAreaViewController *cityVC = [[ChooseAreaViewController alloc]init];
        cityVC.displayType = kDisplayCity;//设置显示模式为城市
        cityVC.citys = citys;
        cityVC.selectedProvince = self.selectedProvince;
        [self.navigationController pushViewController:cityVC animated:NO];

    }else if (self.displayType == kDisplayCity){
        NSDictionary *city = self.citys[indexPath.row];
        self.selectedCity = [city objectForKey:@"name"];
        NSArray *areas = [city objectForKey:@"sub"];
        //构建下一级视图控制器
        ChooseAreaViewController *areaVC = [[ChooseAreaViewController alloc]init];
        areaVC.displayType = kDisplayArea;//设置显示模式为区域
        areaVC.areas = areas;
        areaVC.selectedCity = self.selectedCity;
        areaVC.selectedProvince = self.selectedProvince;
        [self.navigationController pushViewController:areaVC animated:NO];
    }else{
        self.selectedArea = self.areas[indexPath.row];
        self.selectedIndexPath = indexPath;

        NSArray* viewControllers = [self.navigationController childViewControllers];
        ChangeAddressViewController* viewVC = nil;
        for (int i = 0; i<viewControllers.count; i++) {
            if ([viewControllers[i] isKindOfClass:[ChangeAddressViewController class]]) {
                viewVC = viewControllers[i];
                break;
            }
        };
        if (viewVC) {
            [self.navigationController popToViewController:viewVC animated:nil];
        }else{
            [self.navigationController popToRootViewControllerAnimated:YES];
        }

        NSString* receiveStr = [NSString stringWithFormat:@"%@ %@ %@",self.selectedProvince,self.selectedCity,self.selectedArea];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ReceiveStr" object:nil userInfo:@{@"address":receiveStr}];
    }


         [self.tableView reloadData];



    }



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
